package com.leyou.item.service.base;

import com.leyou.common.enums.LyExceptionEnums;
import com.leyou.common.exception.LyException;
import com.leyou.item.mapper.LyBaseMapper;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;


import java.util.List;

public abstract class AbstractBaseService<T> implements BaseService<T> {

    protected abstract LyBaseMapper<T> getMapper();


    @Override
    public List<T> selectAll() {
        return getMapper().selectAll();
    }

    @Override
    public List<T> select(T record) {
        return getMapper().select(record);
    }

    @Override
    public T selectByPrimaryKey(Object key) {
        return getMapper().selectByPrimaryKey(key);
    }

    @Override
    public List<T> selectByExample(Example example) {
        return getMapper().selectByExample(example);
    }

    @Override
    public List<T> selectByIdList(List<Long> idList) {
        return getMapper().selectByIdList(idList);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(T record) {

        int count = getMapper().updateByPrimaryKeySelective(record);
        if(count != 1){
            throw new LyException(LyExceptionEnums.UPDATA_BY_PRIMARY_KEY_FAILURE);
        }
        return count;
    }

    @Override
    @Transactional
    public int deleteByIdList(List<Long> idList) {
        return getMapper().deleteByIdList(idList);
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(Object key) {
        return getMapper().deleteByPrimaryKey(key);
    }

    @Override
    @Transactional
    public int insertList(List<T> recordList) {
        return getMapper().insertList(recordList);
    }

    @Override
    @Transactional
    public int insertSelective(T record) {
        int effect = getMapper().insertSelective(record);
        if (effect != 1) {
            throw new LyException(LyExceptionEnums.INSERT_ERROR);
        }
        return effect;
    }

    @Override
    @Transactional
    public int delete(T record) {
        int effect = getMapper().delete(record);
        if (effect < 1) {
            throw new LyException(LyExceptionEnums.DELETE_FAILURE);
        }
        return effect;
    }
}